<?
/**
 * Copyright 2007 Melange.
 *
 * This file is part of PHP-MELANGE.
 *
 * PHP-MELANGE is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHP-MELANGE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHP-MELANGE; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * @category    Melange
 * @package     php-melange
 * @subpackage  cms
 * @copyright   Copyright (c) 2007 Jeroen Simons. All rights reserved
 * @author      Jeroen Simons <jeroen@melange.nl>
 * @link        http://www.melange.nl/
 *
 */

require_once("mvc/action/Action.php");
require_once("http/Cookie.php");
require_once("cms/core/om/LanguagePeer.php");

class SaveAction extends Action {

    private $log;

    public function __construct() {
        $this->log =& LoggerManager::getLogger("SaveAction");
    }

    public function execute(ActionConfig &$mapping, &$form, Request &$request, Response &$response) {

        if($form->getMore() == "1")
            return $mapping->findForward("success");

        $messages = new ActionMessages();
        $errors   = new ActionMessages();

        // Add a i18n cookie.
        $response->addCookie(
            new Cookie(
                _Core::COOKIE_I18N,
                $form->getCode(),
                time()+60*60*24*30*356,
                "/"
            )
        );

        // Set the CMS locale.
        $session = $request->getSession();
        $session->setAttribute(
            MVC::LOCALE_KEY, new Locale($form->getCode())
        );

        // Handle the Content Languages.
        $codes = $form->getCodes();

        // Add the default language
        $codes[] = $request->getContext()->
                    getInitParameter(_Core::DEFAULT_LANGUAGE);

        $c = new Criteria();
        $c->add(LanguagePeer::I18N, false);
        $c->add(LanguagePeer::CODE, "", Criteria::NOT_EQUAL);
        LanguagePeer::doUpdate($c);

        $c->add(LanguagePeer::CODE, $codes, Criteria::IN);
        $c->add(LanguagePeer::I18N, true);
        try {
            LanguagePeer::doUpdate($c);
        } catch(PropelException $e) {
            $errors->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("error.languages.error.activate", true));
            $this->log->error($e->getMessage());
        }

        // Clear the language cache
        require_once 'Zend/Cache.php';
        $cache = Zend_Cache::factory('Output', 'File', array(), array('cache_dir' => BASEPATH . "/_cache/"));
        $cache->clean(Zend_Cache::CLEANING_MODE_MATCHING_TAG, array('language'));

        $form->setCodes($codes);

        if(!$errors->isEmpty()) {
            $this->saveErrors($request, $errors);
            return $mapping->getInputForward();
        }

        $messages->add(ActionMessage::GLOBAL_MESSAGE,
                new ActionMessage("message.save", true));

        $this->saveMessages($request, $messages);

        return $mapping->findForward("success");

    }

}
?>